package org.jeecg.modules.simulator.rooter;

import org.jeecg.modules.simulator.Simulator;
import org.jeecg.modules.simulator.jsonot.NumPeopEnStationApiInfo;
import org.jeecg.modules.simulator.model.SceneDataType;
import org.jeecg.modules.simulator.model.line.Line;
import org.jeecg.modules.simulator.model.line.park.Parker;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.orbit.OrbitPath;
import org.jeecg.modules.simulator.model.orbit.OrbitPoint;
import org.jeecg.modules.simulator.model.train.timeplan.TrainPlanTable;
import lombok.Getter;
import lombok.Setter;
import org.jeecg.modules.simulator.recorder.Reacorder;
import org.jeecg.modules.simulator.vo.TrainPosition;


import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: gp2
 * @description:
 * @author: renzhg
 * @create: 2024-08-30 11:05
 **/
public class LineRooter implements Simulator {
    @Setter
    @Getter
    String sceneCode;
//    @Setter
//    @Getter
//    String lineId;
    @Getter
    TrainManager trainManager=new TrainManager();
//    @Getter
//    String earliestStartTime;
    @Setter
    @Getter
    boolean isLoaderdata=false;

    private Map<String, List<TrainPlanTable>> trainPlanTableMap=new HashMap<>();

//    @Getter
//    Map<String, Integer> netAllPersonSizePSecMap = new HashMap<>();

    @Setter
    @Getter
    private Map<String,Station> stationInThisLineMap=new HashMap<>();// //以名字为key的stationmap

    @Setter
    @Getter
    private Map<String,Station> stationStionIdMap=new HashMap<>(); //以id为key的stationmap
    LocalTime currentTime ;
    @Setter
    @Getter
    Line line;

    @Setter
    @Getter
    LineInfoPublisher lineInfoPublisher=new LineInfoPublisher();
//    @Setter
//    @Getter
//    Reacorder reacorder=new Reacorder();



    @Getter
    Map<String,Parker> parkerMap=new HashMap<>();//当前线路的场段


//    public void setNetAllPersonSizePSecMap(Map<String, Integer> netAllPersonSizePSecMap) {
////        this.netAllPersonSizePSecMap = netAllPersonSizePSecMap;
//        this.line.setNetAllPersonSizePSecMap(netAllPersonSizePSecMap);
//    }

//    LineStatistics lineStatistics;



    //设定时刻表
    public void setTrainPlanTableMap( List<TrainPlanTable> trainPlanTableMap) {
        this.trainPlanTableMap =trainPlanTableMap.stream().collect(Collectors.groupingBy(o -> o.getTrainId() + "-" + o.getTrainNum()));
    }

    public void setStationMap(Map<String, Station> stationMap) {
        this.line.setStationMap(stationMap);
    }

    public void setParkerMap(Map<String, Parker> parkerMapMap) {
        this.line.setParkerMap(parkerMapMap);
    }


    //region 统计线路信息
    //统计当前线路内不在线列车数




    //endregion

     public String getEarliestStartTime(){
        return trainManager.getEarliestStartTime();
     }
    //region 每秒发布信息




    //endregion





    //设定站点业务参数：1、进站里面人数的OD概率 2、每秒进站人数
    public void setStationBizParam(
            Map<String, Map<String, NumPeopEnStationApiInfo>> PersonInputStationPerPSecMap){
        line.setNumPeopEnStationApiInfoMap(PersonInputStationPerPSecMap);
//        line.setOdProbabilityApiInfoMap(odProbabilityApiInfoMap);

    }








    public String getLineId() {
        return line.getLineId();
    }
    public OrbitPath getUpOrbitPath( ) {
        return this.line.getUpOrbitPath();
    }

    public OrbitPath etDownOrbitPath( ) {
        return this.line.getUpOrbitPath();
    }


    /**
     * @description: 按车次号分的时刻表
     *
     * @author:  renzhg
     * @date:  2024/9/5 17:46
     **/
    public void setTrainPlanTableMap(Map<String, List<TrainPlanTable>> trainPlanTableMap) {
        trainManager.initTrainPlanTablePathMap(trainPlanTableMap);
    }



    /**
     * @description: 从一个上行的轨道编号初始化一个下行的初始化数据
     *
     * @author:  renzhg
     * @date:  2024/9/5 10:45
     **/
    public  List<OrbitPoint> deepCloneOrbitPointList(List<OrbitPoint> originalList) {
        List<OrbitPoint> clonedList = new ArrayList<>();
        for (OrbitPoint point : originalList) {
            clonedList.add((OrbitPoint) point.clone());
        }
        return clonedList;
    }
    /**
     * @description: 初始化
     *
     * @author:  renzhg
     * @date:  2024/9/5 10:45
     **/
    public void assingObtiPath(List<OrbitPoint> orbitPointList){
        List<OrbitPoint> downOrbitPointList = deepCloneOrbitPointList(orbitPointList);
        reverseIndex(downOrbitPointList);

        this.line.setUpOrbitPath(new OrbitPath(orbitPointList));
        this.line.setDownOrbitPath(new OrbitPath(downOrbitPointList));

//        this.line.initOrbitPath();
    }
    //上行轨道序列反向
    public  void reverseIndex(List<OrbitPoint> obs) {
        int size = obs.size();
        for (int i = 0; i < size; i++) {
            // 将原来的 index 赋值到对应的反向位置
            obs.get(i).setIndex(size - 1 - i);
            if(obs.get(i).getOrbitName().contains("-")){
                String[] split = obs.get(i).getOrbitName().split("-");
                obs.get(i).setOrbitName(split[1]+"-"+split[0]);
                String[] split2=obs.get(i).getStationId().split("-");
                obs.get(i).setStationId(split2[1]+"-"+split2[0]);
            }
            long temp = obs.get(i).getStartOffsetDistance();
            obs.get(i).setStartOffsetDistance(obs.get(i).getEndOffsetDistance());
            obs.get(i).setEndOffsetDistance(temp);

        }
    }

/** 
* @description:
 * 列车时刻表装配
 * 列车时刻表转化为列车动作路径
 * 把列车摆放到响应的位置，如果是直接场段开始的就放在场段，如果是线网，则放在线网上
 * 同时把相应的列车动作表调整到对应位置上
*
* @author: renzhg
* @date: 2024/10/6 19:32
**/
    public void initTrainManagerData() {
        System.out.println("初始化线路");
        //初始化列车管理器的上下行轨道
        trainManager.setDownOrbitPath(line.getDownOrbitPath());
        trainManager.setUpOrbitPath(line.getUpOrbitPath());
        trainManager.setWorkOrbitPath();
        trainManager.setLineId(line.getLineId());
        //打印本地数据列车信息
        trainManager.getTrainStateStatisticser().setTrainMap(trainManager.getTrainMap());
        //列车时刻表装配
        //列车时刻表转化为列车动作路径
        //把列车摆放到响应的位置，如果是直接场段开始的就放在场段，如果是线网，则放在线网上
        //同时把相应的列车动作表调整到对应位置上

//        trainManager.assignTrainManager(startTime,sceneDataType);



    }
//    public void initTrainManager(){
//        System.out.println("初始化线路");
//        //初始化列车管理器的上下行轨道
//        trainManager.setDownOrbitPath(line.getDownOrbitPath());
//        trainManager.setUpOrbitPath(line.getUpOrbitPath());
//        trainManager.setWorkOrbitPath();
//        trainManager.setLineId(line.getLineId());
//        trainManager.getTrainStateStatisticser().setTrainMap(trainManager.getTrainMap());
//    }
//    public void initTrainManagerPeriodTimeData(LocalTime startTime, SceneDataType sceneDataType){
//        System.out.println("初始化线路");
//        //初始化列车管理器的上下行轨道
//        trainManager.setDownOrbitPath(line.getDownOrbitPath());
//        trainManager.setUpOrbitPath(line.getUpOrbitPath());
//        trainManager.setWorkOrbitPath();
//        trainManager.setLineId(line.getLineId());
//        trainManager.getTrainStateStatisticser().setTrainMap(trainManager.getTrainMap());
//
//    }





    @Override
    public LocalTime getCurrentTime() {
        return this.currentTime;
    }
    @Override
    public void UpdateCurrentTime(LocalTime currentTime) {
        this.currentTime=currentTime;
    }
    @Override
    public void nextTime(String now, Integer step) {
        this.line.nextTime(now,step);
        this.trainManager.nextTime(now,step);
//        Map<String, TrainPosition> trainPositionMap=this.trainManager.getAllTrainPosition(now);


    }
}
